// ActionScript file
import flash.events.MouseEvent;

import mx.collections.XMLListCollection;
import mx.controls.Alert;
import mx.events.MenuEvent;

import org.springextensions.actionscript.context.support.FlexXMLApplicationContext;

private function init():void
{
    initApplicationContext();
    initMenuBar();
    this.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
}

private function handleMouseMove(event:MouseEvent):void
{
    drawCanvas.dispatchMouseEventToChildren(event);
}


///////////////////////////   Application context //////////////////////////////

private var applicationContext:FlexXMLApplicationContext;

private function initApplicationContext():void
{
    applicationContext = new FlexXMLApplicationContext("application-context.xml");
    applicationContext.addEventListener(Event.COMPLETE, completeHandler);
    applicationContext.load();
    
}

private function completeHandler(event:Event):void
{
    applicationContext.removeEventListener(Event.COMPLETE, completeHandler);
    /*            var button:Button = applicationContext.getObject("testButton") as Button;
    FlexGlobals.topLevelApplication.addElement(button);*/
    //applicationContext.getObject("controller");
    //interpreter.applicationContext = applicationContext;
}
///////////////////////////  End of Application context ////////////////////////


/////////////////////////////////  Menu Bar  ///////////////////////////////////

[Bindable]
public var menuBarCollection:XMLListCollection;

private var menubarXML:XMLList = 
    <>
        <menuitem label="File" data="file">
            <menuitem label="Open" data="open"/>
            <menuitem label="Save" data="save"/>
            <menuitem label="Exit" data="open"/>
        </menuitem>
        <menuitem label="View" data="view">
            <menuitem label="Tools in Drawing" data="toolsOfDraw"/>
            <menuitem label="Canvas in Drawing" data="canvasOfDraw"/>
            <menuitem label="Properties in Drawing" data="propertiesOfDraw"/>
        </menuitem>
        <menuitem label="Level" data="level">
            <menuitem label="Draw" data="draw"/>
            <menuitem label="Mark" data="mark"/>
            <menuitem label="Solve" data="solve"/>
        </menuitem>
    </>;

// Event handler to initialize the MenuBar control.
private function initMenuBar():void {
    menuBarCollection = new XMLListCollection(menubarXML);
}

// Event handler for the MenuBar control's itemClick event.
private function menuHandler(evt:MenuEvent):void  {

/*    if (evt.item.@data != "top") {
        Alert.show("Label: " + evt.item.@label + "\n" + 
            "Data: " + evt.item.@data, "Clicked menu item");
    }*/
    if (evt.item.@data == "draw")
        mainViewStack.selectedChild=drawCanvas;
    else if (evt.item.@data == "mark")
        mainViewStack.selectedChild=markCanvas;
    else if (evt.item.@data == "solve")
        mainViewStack.selectedChild=solveCanvas;
    else if (evt.item.@data == "toolsOfDraw")
    {
        mainViewStack.selectedChild=drawCanvas;
        toolsOfDraw.setVisible(true);
    }
    else if (evt.item.@data == "canvasOfDraw")
    {
        mainViewStack.selectedChild=drawCanvas;
        canvasOfDraw.setVisible(true);
    }
    else if (evt.item.@data == "propertiesOfDraw")
    {
        mainViewStack.selectedChild=drawCanvas;
        propertiesOfDraw.setVisible(true);
    }
}
/////////////////////////////////  End of Menu Bar  ////////////////////////////
